import api from '@MODULES/common/api'
import store from 'store'
import {
    Modal
} from 'antd-mobile';

const alert = Modal.alert;

const common = {
    // 获取用户个人信息
    userInfoUpData: (success) => {
        api.personDes({
            success: (data) => {
                store.set('user', data.user)
                if (success) {
                    success(data.user)
                }
            }
        })
    },
    // 处理输入后页面不会回调
    inputBlur: () => {
        let windowFocusHeight = window.innerHeight
        // if (windowHeight == windowFocusHeight) {
        //     return
        // }
        // console.log(windowHeight + '--' + windowFocusHeight);
        console.log('修复');
        let currentPosition;
        let speed = 1; //页面滚动距离
        currentPosition = document.documentElement.scrollTop || document.body.scrollTop;
        currentPosition -= speed;
        window.scrollTo(0, currentPosition); //页面向上滚动
        currentPosition += speed; //speed变量
    },
    // 默认头像
    showFace: (src) => {
        return src || require('@MODULES/common/img/static/deFace.jpeg')
    },
    // 默认图片
    showImg: (src) => {
        return src || require('@MODULES/common/img/static/contentDefault.png')
    },
    // 判断是否登录
    userIsLogin: (callBack) => {
        if (store.get('user')) {
            if (callBack) {
                callBack()
            }
        } else {
            alert('很抱歉', '您还未登录', [{
                text: '取消',
                onPress: () => console.log('cancel'),
                style: 'default'
            },
            {
                text: '去登录',
                onPress: () => {
                    window.location.href = '/login/goBack'
                }
            },
            ])
        }
    },
    // 长列表获取高度——第一个参数是class，数组形式
    liveViewHeightCalc: (classArr = [], haveHeader = true) => {
        // 获取窗口高度
        var liveViewHeight = document.documentElement.clientHeight
        if (haveHeader) {
            // 窗口高度减去顶部导航高度
            liveViewHeight = liveViewHeight - 45
        }
        classArr.map(item => {
            var dom = document.getElementsByClassName(item)[0]
            // 元素高度
            liveViewHeight = liveViewHeight - dom.offsetHeight
            // 减去元素marginTop
            liveViewHeight = liveViewHeight - parseInt(getComputedStyle(dom).marginTop)
            liveViewHeight = liveViewHeight - parseInt(getComputedStyle(dom).marginBottom)
        })
        return liveViewHeight
    },
    // 将数字转换为汉字
    numberChangeToChinese: (num) => {
        num = num * 1
        switch (num) {
            case 0:
                num = "零";
                break;
            case 1:
                num = "一";
                break;
            case 2:
                num = "二";
                break;
            case 3:
                num = "三";
                break;
            case 4:
                num = "四";
                break;
            case 5:
                num = "五";
                break;
            case 6:
                num = "六";
                break;
            case 7:
                num = "七";
                break;
            case 8:
                num = "八";
                break;
            case 9:
                num = "九";
                break;
        }

        return num
    },
    // 计算百分比
    calcPercentageFun: (purchased, surplus) => {
        purchased = purchased * 1
        surplus = surplus * 1

        return (purchased / (surplus)) * 100
    },
    // 时间格式转换 
    // formatDate(params.start_time, "yyyy-MM-dd HH:mm:ss")
    formatDate: (source, format) => {
        source = new Date(source);
        const o = {
            //     'Y+': source.getFullYear(),
            'M+': source.getMonth() + 1, // 月份
            'd+': source.getDate(), // 日
            'H+': source.getHours(), // 小时
            'm+': source.getMinutes(), // 分
            's+': source.getSeconds(), // 秒
            'q+': Math.floor((source.getMonth() + 3) / 3), // 季度
            'f+': source.getMilliseconds() // 毫秒
        }
        if (/(y+)/.test(format)) {
            format = format.replace(RegExp.$1, (source.getFullYear() + '').substr(4 - RegExp.$1.length))
        }
        for (let k in o) {
            if (new RegExp('(' + k + ')').test(format)) {
                format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
            }
        }
        return format
    },
    // 解析路由参数
    parseUrl: (search, name) => {
        let result = {}
        if (search === '') {
            return null
        }
        let arr = search.substr(1).split('&')
        arr.forEach(item => {
            result[item.split("=")[0]] = item.split("=")[1]
        })
        return result ? result[name] : null
    },
}
export default common;
